package com.ruoyi.schoolSys.service.impl;

import java.sql.Time;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.mysql.cj.util.TimeUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.schoolSys.domain.AttendTime;
import com.ruoyi.schoolSys.mapper.AttendTimeMapper;
import com.ruoyi.schoolSys.model.vo.StudentAttendanceVo;
import com.ruoyi.schoolSys.service.IAttendTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.schoolSys.mapper.StudentAttendanceMapper;
import com.ruoyi.schoolSys.domain.StudentAttendance;
import com.ruoyi.schoolSys.service.IStudentAttendanceService;

/**
 * 学生签到管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-21
 */
@Service
public class StudentAttendanceServiceImpl implements IStudentAttendanceService 
{
    @Autowired
    private StudentAttendanceMapper studentAttendanceMapper;
    @Autowired
    private AttendTimeMapper attendTimeMapper;

    /**
     * 查询学生签到管理
     * 
     * @param id 学生签到管理主键
     * @return 学生签到管理
     */
    @Override
    public StudentAttendanceVo selectStudentAttendanceById(Long id)
    {
        return studentAttendanceMapper.selectStudentAttendanceById(id);
    }

    /**
     * 查询学生签到管理列表
     * 
     * @param studentAttendance 学生签到管理
     * @return 学生签到管理
     */
    @Override
    public List<StudentAttendance> selectStudentAttendanceList(StudentAttendance studentAttendance)
    {
        return studentAttendanceMapper.selectStudentAttendanceList(studentAttendance);
    }

    /**
     * 新增学生签到管理
     * 
     * @param studentAttendance 学生签到管理
     * @return 结果
     */
    @Override
    public int insertStudentAttendance(StudentAttendance studentAttendance)
    {
        studentAttendance.setCreateTime(DateUtils.getNowDate());
        studentAttendance.setSignTime(new Date());
        List<AttendTime> attendTimes = attendTimeMapper.selectAttendTimeList(null);
        //签到
        if(studentAttendance.getSignType()==1){
            if(attendTimes.size()>0){
                Time attendTime = attendTimes.get(0).getAttendTime();

                //上课时间
                DateTime attendDateTime = new DateTime(attendTime);
                //现在时间
                DateTime nowDateTime = new DateTime(LocalDateTime.now());


                if(attendDateTime.hour(true)>nowDateTime.hour(true)){
                    //正常
                    studentAttendance.setAttendanceType("正常");
                } else if (attendDateTime.hour(true)==nowDateTime.hour(true)) {
                    //
                    if(attendDateTime.minute()>nowDateTime.minute()){
                        //正常
                        studentAttendance.setAttendanceType("正常");
                    }else if(attendDateTime.minute()==nowDateTime.minute()){
                        if(attendDateTime.second()>=nowDateTime.second()){
                            studentAttendance.setAttendanceType("正常");
                        }else{
                            studentAttendance.setAttendanceType("迟到");
                        }
                    }else{

                        studentAttendance.setAttendanceType("迟到");
                    }
                }else{
                    //迟到
                    studentAttendance.setAttendanceType("迟到");
                }


            }
            //签退
        }else{
            if(attendTimes.size()>0){
                Time attendTime = attendTimes.get(0).getOverTime();


                //放学时间
                DateTime overDateTime = new DateTime(attendTime);
                //现在时间
                DateTime nowDateTime = new DateTime(LocalDateTime.now());


                if(overDateTime.hour(true)>nowDateTime.hour(true)){
                    //早退
                    studentAttendance.setAttendanceType("早退");
                } else if (overDateTime.hour(true)==nowDateTime.hour(true)) {
                    //
                    if(overDateTime.minute()>nowDateTime.minute()){
                        //正常
                        studentAttendance.setAttendanceType("早退");
                    }else if(overDateTime.minute()==nowDateTime.minute()){
                        if(overDateTime.second()>nowDateTime.second()){
                            studentAttendance.setAttendanceType("早退");
                        }else{
                            studentAttendance.setAttendanceType("正常");
                        }
                    }else{

                        studentAttendance.setAttendanceType("正常");
                    }
                }else{
                    //正常
                    studentAttendance.setAttendanceType("正常");
                }

            }
        }

        return studentAttendanceMapper.insertStudentAttendance(studentAttendance);
    }

    /**
     * 修改学生签到管理
     * 
     * @param studentAttendance 学生签到管理
     * @return 结果
     */
    @Override
    public int updateStudentAttendance(StudentAttendance studentAttendance)
    {
        studentAttendance.setUpdateTime(DateUtils.getNowDate());
        return studentAttendanceMapper.updateStudentAttendance(studentAttendance);
    }

    /**
     * 批量删除学生签到管理
     * 
     * @param ids 需要删除的学生签到管理主键
     * @return 结果
     */
    @Override
    public int deleteStudentAttendanceByIds(Long[] ids)
    {
        return studentAttendanceMapper.deleteStudentAttendanceByIds(ids);
    }

    /**
     * 删除学生签到管理信息
     * 
     * @param id 学生签到管理主键
     * @return 结果
     */
    @Override
    public int deleteStudentAttendanceById(Long id)
    {
        return studentAttendanceMapper.deleteStudentAttendanceById(id);
    }
}
